<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Db;

use app\common\model\Category as CategoryModel;
use app\admin\model\LitestoreGoods;
use app\admin\model\Recovery;
use app\admin\model\Address;
use app\admin\model\Repair;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->user_id = $this->auth->id;
        // $this->user_id = 1;
    }  

    /**
     * 分类列表
     * @ApiSummary  ()
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/category)
     * @ApiReturn   ({
         <pre>array(3) {
          [0] =&gt; array(2) {
            ["type_name"] =&gt; string(6) "手机"//第一级分类
            ["type_category"] =&gt; array(4) {
              [0] =&gt; array(4) {
                ["name"] =&gt; string(6) "苹果"//第二级分类
                ["id"] =&gt; int(14)
                ["type"] =&gt; string(5) "phone"
                ["goods"] =&gt; array(2) {
                  [0] =&gt; array(4) {
                    ["cover"] =&gt; string(70) "http://phone.com/uploads/20200609/e392343f3d22004be8e9c9f92f122dfd.png"
                    ["pid"] =&gt; int(14)
                    ["goods_name"] =&gt; string(10) "Iphone SE2"
                    ["goods_id"] =&gt; int(5)
                  }
                  [1] =&gt; array(4) {
                    ["cover"] =&gt; string(70) "http://phone.com/uploads/20200609/e392343f3d22004be8e9c9f92f122dfd.png"
                    ["pid"] =&gt; int(14)
                    ["goods_name"] =&gt; string(10) "Iphone SE2"
                    ["goods_id"] =&gt; int(1)
                  }
                }
              }
              [1] =&gt; array(4) {
                ["name"] =&gt; string(6) "小米"
                ["id"] =&gt; int(19)
                ["type"] =&gt; string(5) "phone"
                ["goods"] =&gt; array(0) {
                }
              }
              [2] =&gt; array(4) {
                ["name"] =&gt; string(6) "华为"
                ["id"] =&gt; int(18)
                ["type"] =&gt; string(5) "phone"
                ["goods"] =&gt; array(0) {
                }
              }
              [5] =&gt; array(4) {
                ["name"] =&gt; string(6) "魅族"
                ["id"] =&gt; int(20)
                ["type"] =&gt; string(5) "phone"
                ["goods"] =&gt; array(0) {
                }
              }
            }
          }
          [1] =&gt; array(2) {
            ["type_name"] =&gt; string(9) "笔记本"
            ["type_category"] =&gt; array(1) {
              [4] =&gt; array(4) {
                ["name"] =&gt; string(6) "联想"
                ["id"] =&gt; int(16)
                ["type"] =&gt; string(8) "computer"
                ["goods"] =&gt; array(0) {
                }
              }
            }
          }
          [2] =&gt; array(2) {
            ["type_name"] =&gt; string(6) "平板"
            ["type_category"] =&gt; array(1) {
              [3] =&gt; array(4) {
                ["name"] =&gt; string(4) "Ipad"
                ["id"] =&gt; int(17)
                ["type"] =&gt; string(4) "flat"
                ["goods"] =&gt; array(0) {
                }
              }
            }
          }
        }
        </pre>
        })
    
     */
    public function category(){
        
        $good_data = Db::name('litestore_goods')->field('cover,pid,goods_name,goods_id')->order('weigh desc')->select();

        $category = Db::name('category')->field('name,id,type')->order('weigh desc')->select();

        $typeList = CategoryModel::getTypeList();
        foreach ($typeList as $key => $value) {
          $GLOBALS['now_type'] = $key;

          $now_category = array_filter($category,function ($val){

              if($val['type']==$GLOBALS['now_type']){
                return $val;
              }
          });

          foreach ($now_category as $k => $v) {
            $GLOBALS['now_category_id'] = $v['id'];
            
            $now_goods_data = array_filter($good_data,function ($val){

                if($val['pid']==$GLOBALS['now_category_id']){
                  return $val;
                }
            });

            $now_category[$k]['goods'] = $now_goods_data;

          }

          $new_arr[] = ['type_name'=>$value,'type_category'=>$now_category];

        }

        $this->success('首页数据',$new_arr);
    }

      /**
     * 首页获取数据
     * @ApiSummary  (首页推荐,轮播图)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/index)
     * @ApiReturn   ({
         {
              "code": 1,
              "msg": "首页数据",
              "time": "1592320333",
              "data": {
                "banner": [
                  {
                    "id": 13,
                    "image": "/assets/img/qrcode.png",
                    "createtime": 1592318345,
                    "updatetime": 1592318345
                  },
                  {
                    "id": 12,
                    "image": "http://phone.com/uploads/20200609/e392343f3d22004be8e9c9f92f122dfd.png",
                    "createtime": 1586749361,
                    "updatetime": 1592318340
                  }
                ],
                "hot_data": [
                  {
                    "goods_id": 9,
                    "cover": "http://phone.com/uploads/20200609/e392343f3d22004be8e9c9f92f122dfd.png",
                    "goods_name": "iPhone手机",
                    "show_money": 7783
                  },
                  {
                    "goods_id": 10,
                    "cover": "http://phone.com/uploads/20200609/e392343f3d22004be8e9c9f92f122dfd.png",
                    "goods_name": "iPhone手机",
                    "show_money": 6688
                  }
                ]
              }
            }
        })
    
     */
    public function index(){
        $banner = Db::name('banner')->order('updatetime desc')->select();

        $hot_data = Db::name('litestore_goods')->order('updatetime desc')->field('goods_id,cover,goods_name,show_money')->order('updatetime desc')->select();

        $return['banner'] = $banner;
        $return['hot_data'] = $hot_data;

        $this->success('首页数据',$return);
    }

    /**
     * 手机回收提交
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/recovery)
     * @ApiParams   (name="phone_model",type=int,required=false,description="手机型号")
     * @ApiParams   (name="memory",type=string,required=false,description="手机内存")
     * @ApiParams   (name="color",type=string,required=false,description="手机颜色")
     * @ApiParams   (name="phone",type=int,required=false,description="手机号码")
     * @ApiParams   (name="address",type=string,required=false,description="地址")
     * @ApiParams   (name="name",type=int,required=false,description="名称")
     * @ApiReturn ({
        
     })
     */
    public function recovery(){
        
        $params = input();
        $params['user_id'] = $this->user_id;

        $Recovery = new Recovery;
        $res = $Recovery->save($params);
        if($res)$this->success('提交成功');
    }

     /**
     * 故障维修提交
     * @ApiSummary  ()
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/repair)
     * @ApiParams   (name="goods_id",type=int,required=true,description="商品id")
     * @ApiParams   (name="content",type=string,required=false,description="故障内容")
     * @ApiParams   (name="phone",type=int,required=false,description="手机号码")
     * @ApiParams   (name="address",type=string,required=false,description="地址")
     * @ApiParams   (name="name",type=int,required=false,description="名称")
     * @ApiReturn ({
  
     })
     */
    public function repair(){
        
        $params = input();
        $params['user_id'] = $this->user_id;

        $Repair = new Repair;
        $res = $Repair->save($params);
        if($res)$this->success('提交成功');
    }

    /**
     * 我的收货地址列表 isdefault等于1为默认地址
     */
    public function address_list(){
      $data = Db::name('address')->where(['user_id'=>$this->user_id])->order('isdefault desc,updatetime desc')->select();
      $this->success('收货地址列表',$data);
    }

    /**
     * 收货地址详情
     * @param int $address_id
     */
    public function address_detail(){
      $data = Db::name('address')->where(['id'=>input('address_id')])->find();
      $this->success('收货地址详情',$data);
    }
    /**
     * 收货地址删除
     * @param int $address_id
     */
    public function address_delete(){
      $now_data = Db::name('address')->where(['id'=>input('address_id')])->find();
      if($now_data['isdefault']==1){
        $this->error('默认收货地址无法删除');
      }
      $data = Db::name('address')->where(['id'=>input('address_id')])->delete();
      if($data)$this->success('操作成功');
      $this->error('操作失败');
    }

     /**
     * 收货地址添加,修改以及设置默认收货地址。修改和设置默认情况传 address_id 过来。添加时候不用传address_id。
     * @ApiMethod   (POST)
     * @ApiRoute    (/api/index/action_address)
     * @ApiParams   (name="phone",type=int,required=false,description="手机号码")
     * @ApiParams   (name="name",type=string,required=false,description="收货人姓名")
     * @ApiParams   (name="detail",type=string,required=false,description="详细地址")
     * @ApiParams   (name="province",type=int,required=false,description="省")
     * @ApiParams   (name="city",type=string,required=false,description="市")
     * @ApiParams   (name="region",type=int,required=false,description="区")
     * @ApiParams   (name="address_id",type=int,required=false,description="收货地址id")
     * @ApiParams   (name="action",type=string,required=false,description="修改时候传edit,设置默认时候不用传")
     */
    public function action_address(){
        
        $params = input();
        $params['user_id'] = $this->user_id;

        if($params['address_id']){
          $pass_address_data = Address::get($params['address_id']);
          if(empty($pass_address_data))$this->error('记录为空');

          if($params['action']=='edit'){
            $params['user_id'] = $this->user_id;
            $pass_address_data->allowField(true)->save($params);
          }else{
            
            Db::name('address')->where(['user_id'=>$this->user_id])->update(['isdefault'=>0]);
            $pass_address_data->save(['isdefault'=>1]);
          }
        }else{
          $exist_data = Db::name('address')->where(['user_id'=>$this->user_id])->find();
          if(empty($exist_data))$params['isdefault']=1;       
          $Address = new Address($params);
          $Address->allowField(true)->save();
        }

        $this->success('操作成功');

    }

     /**
     * 手机详情页面
     * @ApiSummary  (spec_type 20:多规格/10:单规格。)
     * @ApiMethod   (GET)
     * @ApiRoute    (/api/index/goods_detail)
     * @ApiParams   (name="goods_id",type=int,required=true,description="手机id")
     * @ApiReturn ({
       
     })
     */
    public function goods_detail(){
        $goods_id = $this->request->request('goods_id');
        if(empty($goods_id))$this->error('缺少手机id');
        
        $LitestoreGoods = new LitestoreGoods;
        $detail = $LitestoreGoods->detail($goods_id, ['specRel', 'spec', 'spec_rel.spec']);
        if (!$detail)$this->error('很抱歉，商品信息不存在或已下架');
        // 规格信息
        $specData= $LitestoreGoods->getApiManySpecData($detail['spec_rel'], $detail['spec']);

        $detail = $detail->toArray();
        if($detail['spec_type']=='20'){
          unset($detail['spec_rel']);
          // unset($detail['spec']);
       
          $data = array_merge($detail,$specData);
        }else{

          $data = $detail;
        }
        
        $this->success('请求成功',$data);

    }

    

  

}
